/*
** map.c for  in /u/all/tessa_b/public/trantor
** 
** Made by arnaud1 tessa
** Login   <tessa_b@epitech.net>
** 
** Started on  Tue Jul  5 13:54:25 2011 arnaud1 tessa
** Last update Thu Jul  7 09:18:01 2011 arnaud1 tessa
*/

#include	<strings.h>
#include	<unistd.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	"map.h"

void		map_init(t_map *map, int x, int y, int max_trantorians)
{
  t_trantorian	**tr;
  t_case	**tmp_map;
  int		i;
  int		size;

  if ((x < 1) && (y < 1))
    {
      fprintf(stderr, "map_init : bad params x or y\n");
      exit(1);
    }
  tmp_map = malloc(x * sizeof(*tmp_map));
  if (tmp_map == NULL)
    {
      fprintf(stderr, "map_init : out of memory\n");
      exit(1);
    }
  for (i = 0; i < x; i++)
    {
      tmp_map[i] = malloc(y * sizeof(**tmp_map));
      if (tmp_map[i] == NULL)
	{
	  fprintf(stderr, "map_init : out of memory\n");
	  exit(1);
	}
    }
  map->x = x;
  map->y = y;
  map->cases = tmp_map;
  size = (max_trantorians + 1) * sizeof(*tr);
  tr = malloc(size);
  if (tr == NULL)
    {
      fprintf(stderr, "case_init : out of memory\n");
      exit(1);
    }
  bzero(tr, size);
  map->trantorians = tr;
  map->nb_trantorians = 0; 
}

void		map_generate_resources(t_map *map, int max_trantorians)
{
  int		i;
  int		j;
  t_case	*c;

  for (i = 0; i < map->x; i++)
    {
      for (j = 0; j < map->y; j++)
	{
	  c = &(map->cases[i][j]);
	  case_init(c, max_trantorians);
	}
    }
}

void		map_add_trantorian(t_map *map, t_trantorian *tr)
{
  int		i;
  t_case	*c;

  i = 0;
  c = &(map->cases[tr->x][tr->y]);
  while (map->trantorians[i] != NULL)
    {
      i++;
    }
  map->trantorians[i] = tr;
  map->nb_trantorians++;
  case_add_trantorian(c, tr);
}

void		map_remove_trantorian(t_map *map, t_trantorian *tr)
{
  int		ind;
  int		i;
  int		nb;
  t_case	*c;

  i = 0;
  c = &(map->cases[tr->x][tr->y]);
  nb = map->nb_trantorians;
  if (nb > 0)
    {
      while (i < nb)
	{
	  if (map->trantorians[i] == tr)
	    {
	      map->trantorians[i] = map->trantorians[nb - 1];
	      map->trantorians[nb - 1] = NULL;
	      map->nb_trantorians--;
	      case_remove_trantorian(c, tr);
	      break;
	    }
	  i++;
	}
    }
  printf("%d\n", map->nb_trantorians);
}

t_resources	*map_resources_at_pos(t_map *map, int x, int y)
{
  t_resources	*res;

  res = &((map->cases[x][y]).resources);
  return (res);
}

t_trantorian	**map_trantorians_at_pos(t_map *map, int x, int y)
{
  t_trantorian	**trants;

  trants = (map->cases[x][y]).trantorians;
  return (trants);
}

int		map_nb_trantorians_at_pos(t_map *map, int x, int y)
{
  int		nb;

  nb = (map->cases[x][y]).nb_trantorians;
  return (nb);
}

void		map_display(t_map *map)
{
  int		i;
  int		j;
  t_case	c;

  for (i = 0; i < map->x; i++)
    {
      for (j = 0; j < map->y; j++)
	{
	  c = map->cases[i][j];
	  printf("%d - %d : %s\n", i, j, case_to_string(&c));
	}
    }
}

/*
int		main(int ac, char **av)
{
  t_map		m;
  t_trantorian	t1;
  t_trantorian	t2;
  
  t1.x = 0;
  t1.y = 0;
  t2.x = 1;
  t2.y = 1;
  if (ac != 3)
    {
      printf("bad params\n");
      exit(1);
    }
  map_init(&m, atoi(av[1]), atoi(av[2]), 42);
  map_generate_resources(&m, 1024);
  map_add_trantorian(&m, &t1);
  map_add_trantorian(&m, &t2);
  map_display(&m);
  printf("-----\n");
  map_remove_trantorian(&m, &t2);
  map_display(&m);
  printf("-----\n");
  map_remove_trantorian(&m, &t1);
  map_display(&m);
  return (0);
}
*/

/*
** cc map.c ../case/case*.c ../resources/resources.c -g
*/
